package com.supermarket.serivce;

import com.supermarket.dao.CommodityDao;
import com.supermarket.entity.CommodityEntity;
import java.util.ArrayList;

public class CommoditySerivce {
    private CommodityDao CommodityDao = new CommodityDao();

    // 查询所有信息
    public ArrayList<CommodityEntity> all() {ArrayList<CommodityEntity> studententities = CommodityDao.all();return studententities;}

    // 根据id查询信息
    public CommodityEntity getById(Long stuId) {return CommodityDao.getById(stuId);}

    // 添加信息
    public int insert(CommodityEntity stu) {
        return CommodityDao.insert(stu);
    }

    // 修改信息
    public int update(CommodityEntity stu) {
        return CommodityDao.update(stu);
    }

    // 删除信息
    public int del(Long id) {return CommodityDao.del(id);}

    // 商品出库
    public int stockOut(Long commodityId, Long quantity) {
        CommodityEntity commodity = CommodityDao.getById(commodityId);
        if (commodity == null || commodity.getNumber() < quantity) {
            System.out.println("库存不足或商品不存在");
            return 0;
        }
        // 减少库存
        commodity.setNumber(commodity.getNumber() - quantity);
        CommodityDao.update(commodity);
        // 添加出库记录
        return CommodityDao.addOutboundRecord(commodity, quantity);
    }

    // 获取商品出库记录
    public ArrayList<CommodityEntity> allOutbound() {
        return CommodityDao.allOutbound();
    }
}
